package leetcode;

/*
 * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
   Output: 7 -> 0 -> 8
 */
public class AddTwoNumbers {
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode c1=l1;
		ListNode c2=l2;
		int sum=0;
		ListNode newnode = new ListNode(0);
		ListNode d=newnode;
		while(c1!=null||c2!=null) {
			sum/=10;
			if (c1!=null) {
			    sum+=c1.val;
			    c1=c1.next;
			}
			if (c2!=null) {
				sum+=c2.val;
				c2=c2.next;
			}
			d.next=new ListNode(sum%10);
			d=d.next;
		}
		return newnode.next;
	}
}

class ListNode {
	int val;
	ListNode next;
	public ListNode(int x) {
		val = x;
	}
}
